use Entity;
use Entities;
use EntityBag;
use Component;

package EntityManager;

@ISA = qw/Component/;

sub new{
    my $class = shift;
    my $self = {};

    bless $self, $class;
    $self->init();

    return $self;
} # new

sub init(){
    my $self = shift;

    $self->{ENTITIES} = EntityBag->new();


    $self->dependencies(qw/DependencyResolver/);

} # init 


## Parameters:
### 1) Type of the entity to be spawned
### 2) Category to be registered into
## If 2) is undefined, the entity is registered to the default category for its type
## If 2) is UNREGISTERED, the entity is NOT REGISTERED
sub spawnEntity{
    my $self = shift;
    my $type = shift;
    my $category = shift;

    my $register = 1 unless ($category eq "UNREGISTERED");
    my $entity = eval "$type->spawn";
    
    $self->{DependencyResolver}->resolveDependencies($entity);

    $self->registerEntity($entity, $category) if($register);

    return $entity;
}

sub registerEntity{
    my $self = shift;
    my ($entity, $category) = (shift, shift);
    
    $self->{ENTITIES}->add($entity, $category);
}

sub unregisterEntity{
   my $self = shift;
   my $entity = shift;

   $self->{ENTITIES}->remove($entity);
}

sub entities{
    my $self = shift;
    return $self->{ENTITIES};
}


1;
